package top.mccat.raven.utils.handler;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import top.mccat.raven.factory.ConfigFactory;
import top.mccat.raven.item.StrengthItem;
import top.mccat.raven.utils.utils.ColorUtils;
import top.mccat.raven.utils.utils.DefaultValue;
import java.util.Arrays;
import java.util.Random;
import java.util.logging.Logger;
import static top.mccat.raven.utils.utils.DefaultValue.STRENGTHTITLE;

/**
 * @ClassName: StrengthCommandHandler
 * @Description: TODO
 * @Author: Raven
 * @Date: 2021/7/7
 * @Version: 1.0
 */
public class StrengthCommandHandler {
    private JavaPlugin plugin;
    private Object[] strengthItem;
    private Player sender;
    private Random chance = new Random();
    private int[] chanceArray = new int[30];
    private boolean debug = false;
    public static final String Title = "§b[强化等级]";
    private String trashName = "&f普通",rareName="&b稀有",epicName="&5史诗",legendName="&e传说";
    private String trashColor = "&f",rareColor = "&b",epicColor="&5",legendColor="&e";
    private ConfigFactory factory;
    private Logger log;

    public StrengthCommandHandler(JavaPlugin plugin){
        this.plugin = plugin;
        log = plugin.getLogger();
        factory = new ConfigFactory(plugin);
        readConfigExtra();
    }

    public void readConfigExtra(){
        if (factory.getConfig().get("setting.debugMode").equals("true")){
            debug = true;
        }
        chanceArray = factory.getLevelArray("levelChance");
        trashName = factory.getColorLevelLore("setting.lore.trashLore");
        trashColor = factory.getColorLevelLore("setting.lore.trashColor");
        rareName = factory.getColorLevelLore("setting.lore.rareLore");
        rareColor = factory.getColorLevelLore("setting.lore.rareColor");
        epicName = factory.getColorLevelLore("setting.lore.epicLore");
        epicName = factory.getColorLevelLore("setting.lore.epicColor");
        legendName = factory.getColorLevelLore("setting.lore.legendLore");
        legendColor = factory.getColorLevelLore("setting.lore.legendColor");
        strengthItem = factory.getStrengthItemName("item.strengthItem");
        log.info(this.toString());
    }

    public boolean onStrengthItem(StrengthItem item,String title,boolean isLuck,boolean isSafe,boolean isSuccess,boolean isAdmin){
        if(canBeStrength(item)){
            ItemStack myStack = null;
            int level = item.getLevel(title);
            if(isAdmin){
                level = 29;
                Bukkit.broadcastMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c&l卑鄙的管理员使用管理员强化指令直接将他的武器强化到了&a[&b30&a]" +
                        "&c&l级，真是厚颜无耻！"));
            }
            if(level == 30){
                sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c你的武器已经强化到满级，无法继续强化！"));
                return false;
            }else {
                String color = "&8";
                if(isSuccess(level) || isSuccess){
                    color = getSuccessColor(level);
                    level++;
                    sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&b恭喜您，强化成功！"));
                    if(level>15 && level< 30){
                        Bukkit.broadcastMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&b玩家&a"+sender.getName()+"&b将自己的武器到了&a[" + level+
                                "]&b级！"));
                    }else if(level==30){
                        Bukkit.broadcastMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&e&l玩家&a"+sender.getName()+"&e&l将自己的武器到了&b[" + level+
                                "]&e&l级！真是可喜可贺！"));
                    }
                }else {
                    if(level <= 5){
                        level++;
                        sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c很遗憾，您的强化失败了，但是五级以前不损失强化等级故没有降级！"));
                    }else if(level>5 && level <=20){
                        if(!isLuck){
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c很遗憾，您的强化失败了，由于没有装备等级保护卷的保护导致强化等级损失！"));
                        }else {
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c很遗憾，您的强化失败了，但是由于装备等级保护卷的保护装备并没有损失等级！"));
                            level++;
                        }
                    }else if(level>20){
                        if(!isSafe){
                            myStack = item.getStrengthstack(item,color,level);
                            myStack.setType(Material.AIR);
                            sender.getInventory().setItemInMainHand(myStack);
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c很遗憾，您的强化失败了，由于没有装备保护卷的保护装备破碎了！"));
                            Bukkit.broadcastMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c玩家&a"+sender.getName()+"&c在强化自己的武器到&b[" + level+
                                    "]&c级时没有准备装备强化保护卷导致武器破碎了！"));
                        }else {
                            sender.sendMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c很遗憾，您的强化失败了，但是由于装备保护卷的保护装备并没有破碎！"));
                            Bukkit.broadcastMessage(ColorUtils.getColorStr(STRENGTHTITLE+"&c玩家&a"+sender.getName()+"&c在强化自己的武器到&b[" + level+
                                    "]&c级时使用了装备强化保护卷导致武器尚存，还好没碎！"));
                        }
                    }
                    color = getFailColor(level);
                    level--;
                }
                myStack = item.getStrengthstack(item,color,level);
                sender.getInventory().setItemInMainHand(myStack);
            }
            return true;
        }else {
            sender.sendMessage(DefaultValue.STRENGTHTITLE+"§b请确定手中物品是否能被强化或背包中有足够的强化石！");
        }
        return false;
    }
    
    private String getSuccessColor(int level){
        if(level>= 4 && level<9){
            return trashColor;
        }else if(level>= 9 && level<19){
            return rareColor;
        }else if(level>= 19 && level<24){
            return epicColor;
        }else if(level>= 25){
            return legendColor;
        }
        return "&8";
    }

    private String getFailColor(int level){
        if(level>=6 && level<11){
            return trashColor;
        }else if(level>=11 && level<21){
            return rareColor;
        }else if(level>=21 && level<26){
            return epicColor;
        }else if(level>=26 && level<31){
            return legendColor;
        }
        return "&8";
    }

    private boolean isSuccess(int level){
        int extra = chance.nextInt(101);
        if(debug){
            log.info("强化几率:"+extra+" 数组几率:"+chanceArray[level]);
        }
        if(extra < chanceArray[level]){
            return true;
        }
        return false;
    }

    public boolean canBeStrength(StrengthItem item){
        for(Object mate : strengthItem){
            if(item.getItemMaterial().toString().equals(mate)){
                return true;
            }
        }
        return false;
    }

    public boolean canBeStrength(ItemStack item){
        for(Object mate : strengthItem){
            if(item.getType().toString().equals(mate)){
                return true;
            }
        }
        return false;
    }

    public void setSender(Player sender) {
        this.sender = sender;
    }

    public void setFactory(ConfigFactory factory) {
        this.factory = factory;
    }

    @Override
    public String toString() {
        return "StrengthCommandHandler{" +
                "plugin=" + plugin +
                ", sender=" + sender +
                ", chance=" + chance +
                ", chanceArray=" + Arrays.toString(chanceArray) +
                ", debug=" + debug +
                ", trashName='" + trashName + '\'' +
                ", rareName='" + rareName + '\'' +
                ", epicName='" + epicName + '\'' +
                ", legendName='" + legendName + '\'' +
                ", trashColor='" + trashColor + '\'' +
                ", rareColor='" + rareColor + '\'' +
                ", epicColor='" + epicColor + '\'' +
                ", legendColor='" + legendColor + '\'' +
                ", factory=" + factory +
                '}';
    }
}
